package com.hxgz.steel.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxgz.steel.common.ServerResponse;
import com.hxgz.steel.dao.CategoryMapper;
import com.hxgz.steel.dao.MerchantMapper;
import com.hxgz.steel.dao.ProductMapper;
import com.hxgz.steel.dao.ProductModelMapper;
import com.hxgz.steel.dao.ProductParameterMapper;
import com.hxgz.steel.domain.Category;
import com.hxgz.steel.dto.GoodsTimeDto;
import com.hxgz.steel.domain.Merchant;
import com.hxgz.steel.domain.MerchantDto;
import com.hxgz.steel.domain.Page;
import com.hxgz.steel.domain.Product;
import com.hxgz.steel.domain.ProductModel;
import com.hxgz.steel.domain.ProductParameter;
import com.hxgz.steel.dto.ProductTimeDto;
import com.hxgz.steel.service.BackendMerchantService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class backendMerchantServiceImp implements BackendMerchantService {

    @Autowired
    MerchantMapper merchantMapper;

    @Autowired
    ProductMapper productMapper;

    @Autowired
    ProductParameterMapper productParameterMapper;

    @Autowired
    ProductModelMapper productModelMapper;

    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public ServerResponse checkBackendMerchant(HashMap<Object, Object> map, Page page) {
        PageHelper.startPage(page.getCurrPageNo(), page.getPageSize());
        List<MerchantDto> list = merchantMapper.checkBackendMerchant(map);
        for (MerchantDto m : list) {
            String[] split = m.getBusinessScope().split("-");
            StringBuilder builder = new StringBuilder();
            for (String str : split) {
                switch (Integer.valueOf(str)) {
                    case 1:
                        builder.append("风类 ");
                        break;
                    case 2:
                        builder.append("水类 ");
                        break;
                    case 3:
                        builder.append("电类 ");
                        break;
                    case 4:
                        builder.append("消防类 ");
                        break;
                }
            }
            m.setBusinessScope("" + builder);
        }
        PageInfo pageInfo = new PageInfo(list);
        return ServerResponse.createBySuccess(pageInfo);
    }


    @Override
    @Transactional
    public ServerResponse updateStatus(String id, Integer status, String annualFee,
                                       String commission) {
        Merchant merchant = merchantMapper.selectByPrimaryKey(Long.valueOf(id));
        String[] split = merchant.getBusinessScope().split("-");
        StringBuilder builder = new StringBuilder();
        for (String str : split) {
            switch (Integer.valueOf(str)) {
                case 1:
                    builder.append("风类 ");
                    break;
                case 2:
                    builder.append("水类 ");
                    break;
                case 3:
                    builder.append("电类 ");
                    break;
                case 4:
                    builder.append("消防类 ");
                    break;
            }
        }
        merchant.setBusinessScope("" + builder);
        if (StringUtils.isNotBlank(annualFee)) {
            merchant.setAnnualFee(annualFee);
        }
        if (StringUtils.isNotBlank(commission)) {
            merchant.setCommission(commission);
        }
        if (status != null) {
            if (status == 0) {
                merchant.setStatus(2);
            } else if (status == 1) {
                merchant.setStatus(1);
            } else if (status == 3) {
                merchant.setStatus(3);
            }
        }
        int i = merchantMapper.updateByPrimary(merchant);
        if (merchant.getProcess() != null && merchant.getProcess() == 1) {
            merchant.setProcessName("是");
        } else {
            merchant.setProcessName("否");
        }
        if (merchant.getSpotGoods() != null && merchant.getSpotGoods() == 1) {
            merchant.setSpotGoodsName("是");
        } else {
            merchant.setSpotGoodsName("否");
        }
        return ServerResponse.createBySuccess(merchant);
    }

    @Override
    public ServerResponse checkGoods(HashMap<Object, Object> map, Page page) {
        PageHelper.startPage(page.getCurrPageNo(), page.getPageSize());
        List<GoodsTimeDto> list = productMapper.checkGoods(map);
        PageInfo pageInfo = new PageInfo(list);
        return ServerResponse.createBySuccess(pageInfo);
    }

    @Override
    public ServerResponse checkGoodsById(String id) {
        Product product = productMapper.selectByPrimaryKey(Long.valueOf(id));
        String photos = productMapper.getSubImagesById(Long.valueOf(id));
        product.setImgs(photos);
        List<ProductParameter> list = productParameterMapper.listParameterByProId(product.getId());
        List<ProductModel> productModels = productModelMapper.listModelsByProId(product.getId());
        ProductTimeDto productTimeDto = new ProductTimeDto();
        Category category = categoryMapper.selectByPrimaryKey(product.getCategoryId());
        product.setProductCate(category.getName());
        productTimeDto.setProduct(product);
        productTimeDto.setList(productModels);
        productTimeDto.setProductParameter(list);
        productTimeDto.setPhotos(photos);
        return ServerResponse.createBySuccess(productTimeDto);
    }
}

